package com.canbot.u05.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.MainApplication;
import com.canbot.u05.R;
import com.canbot.u05.activity.admin.AdminSettingActivity;
import com.canbot.u05.activity.agenda.AgendaHomeActivity;
import com.canbot.u05.activity.eat.EatActivity;
import com.canbot.u05.activity.firstguide.PasswordSetActivity;
import com.canbot.u05.activity.free.FreeSettingsActivity;
import com.canbot.u05.bean.JsonResponse;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.view.CenterCommonHintDialog;
import com.canbot.u05.widget.LoadingDialogUtils;

import java.util.HashMap;


/**
 * 手持pad  其他设置
 */
public class OtherSettingActivity extends BaseActivityNoTitle {
        private static final int ADMIN_REQUEST_CODE = 102;
        private static final int FACTORY_RESET = 103;
        private OtherSettingActivity mContext;

        private ToastTools toastTools;
        private CenterCommonHintDialog resetDialog;
        private Dialog loadingDialog;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.act_other_setting);
                toastTools = ToastTools.newInstance(this);
                mContext = this;
        }

        @Override
        protected void onResume() {
                super.onResume();
        }

        protected void startotherActivity(Context context, Class<?> cls) {
                Intent intent = new Intent(context, cls);
                startActivity(intent);
        }

        public void back(View view) {
                //停止播放音频
                MsgSendUtils.sendStringMsg(MsgType.STOP_SOUND, "stop_sound");
                finish();
        }



        /**
         * 跳转到空闲状态设置
         * @param view
         */
        public void free(View view) {
                startotherActivity(this, FreeSettingsActivity.class);
        }

        /**
         * 跳转到工作日程
         * @param view
         */
        public void taskSchedule(View view) {
                startotherActivity(this, AgendaHomeActivity.class);
        }

        /**
         * 跳转到自动充电
         * @param view
         */
        public void automaticCharging(View view) {
                startotherActivity(this, EatActivity.class);
        }

        /**
         * 跳转到恢复出厂设置
         * @param view
         */
        public void factoryReset(View view) {
                Intent intent = new Intent(OtherSettingActivity.this,PassWordActivity.class);
                startActivityForResult(intent, FACTORY_RESET);
        }

        /**
         * 跳转到管理员设置
         * @param view
         */
        public void manager(View view) {
                Intent intent = new Intent(OtherSettingActivity.this,PassWordActivity.class);
                startActivityForResult(intent, ADMIN_REQUEST_CODE);
        }

        /**
         * 跳转到语音设置
         * @param view
         */
        public void voiceSetting(View view) {
                startotherActivity(this, VoiceSettingActivity.class);
        }

        /**
         *
         * @param requestCode ADMIN_REQUEST_CODE 管理员设置返回
         * @param resultCode  Activity.RESULT_OK 验证成功
         */
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (resultCode == Activity.RESULT_OK){
                        if (requestCode == ADMIN_REQUEST_CODE){
                                startActivity(new Intent(this, AdminSettingActivity.class));
                        }else if (requestCode == FACTORY_RESET){
                                handlerFactoryReset();
                        }

                }
        }

        /**
         * 恢复出厂设置处理
         */
        private void handlerFactoryReset() {
                resetDialog = new CenterCommonHintDialog(this, 10);
                resetDialog.show();
                resetDialog.setListner(id -> {
                        switch (id)  {
                                case R.id.reset_dialog_sure:
                                        deleteAdminInfo();
                                        resetDialog.dismiss();
                                        break;
                                case R.id.reset_dialog_cancel:
                                        resetDialog.dismiss();
                                        break;
                                default:
                                        break;


                        }
                });
        }

        private void deleteAdminInfo() {
                loadingDialog = LoadingDialogUtils.createLoadingDialog(this, getString(R.string.request));
                final HashMap<String,String> params = new HashMap<>();
                params.put("adminType","deleteAll");
                OkHttpUtils.upLoadFile("http://" + IpMsgConst.CHEST_SOCKET_HOST + ":7755/" + "admin", params, new OkHttpUtils.OkHttpCallBack() {
                        @Override
                        public void onSuccess(final String result) {
                                final JsonResponse jsonResponse = JSON.parseObject(result, JsonResponse.class);
                                if (jsonResponse.getRc() == -1){
                                        toastTools.showText(getString(R.string.factory_reset_failed));
                                }else if (jsonResponse.getRc() == 0){
                                        runOnUiThread(() -> {
                                                startActivity(new Intent(OtherSettingActivity.this,PasswordSetActivity.class));
                                                finish();
                                        });
                                }
                                cancelDialog();
                        }

                        @Override
                        public void onFailure() {
                                toastTools.showText(getString(R.string.service_error_hint));
                                cancelDialog();
                        }
                });
        }

        /**
         * 取消加载的Dialog
         */
        private void cancelDialog() {
                runOnUiThread(() -> {
                        if (loadingDialog != null && loadingDialog.isShowing()) {
                                loadingDialog.cancel();
                        }
                });

        }

        @Override
        protected void onPause() {
                super.onPause();
                if (loadingDialog != null && loadingDialog.isShowing()){
                        loadingDialog.cancel();
                }

                if (resetDialog != null && resetDialog.isShowing()){
                        resetDialog.cancel();
                }
        }
}
